package org.wanho.servlet;

import com.alibaba.fastjson.JSON;
import org.wanho.entity.User;
import org.wanho.vo.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-12-03 10:30
 * Description：<描述>
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        System.out.println("---------------" + name);

        //  try {
        //     Thread.sleep(5000); // 休眠
        // } catch (InterruptedException e) {
        //     throw new RuntimeException(e);
        // }

        resp.setContentType("application/json; charset=utf8");
        PrintWriter out = resp.getWriter();

        // User user = new User(1001, "tom", "123", 20, "male", 175.8, new Date());
        //
        // String jsonString = JSON.toJSONString(user);
        // System.out.println(jsonString);
        //
        // String str = "{\"name\":\"alice\",\"age\":18}";
        // User u = JSON.parseObject(str, User.class);
        // System.out.println(u);

        List<User> list = new ArrayList<>();
        list.add(new User(1001, "tom", "123", 20, "male", 175.8, new Date()));
        list.add(new User(1002, "jack", "456", 22, "female", 165.8, new Date()));
        list.add(new User(1003, "smith", "789", 23, "male", 165.8, new Date()));

        // 创建一个响应结果对象
        AjaxResult result = new AjaxResult();
        result.setStatus(200);
        result.setMsg("获取用户数据成功");
        result.setData(list);


        out.print(JSON.toJSONString(result));
        out.close();
    }
}
